package com.llgz.hotelmanageapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llgz.hotelmanageapi.pojo.DO.CheckinInfo;
import com.llgz.hotelmanageapi.pojo.DO.CheckoutInfo;
import com.llgz.hotelmanageapi.pojo.DO.OrderInfo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.llgz.hotelmanageapi.mapper.OrderInfoMapper;
import com.llgz.hotelmanageapi.mapper.RoomInfoMapper;
import com.llgz.hotelmanageapi.pojo.DAO.RoomReserveInfoDAO;
import com.llgz.hotelmanageapi.pojo.DO.OrderInfo;
import com.llgz.hotelmanageapi.pojo.DO.RoomInfo;
import com.llgz.hotelmanageapi.pojo.DO.RoomReserveInfo;
import com.llgz.hotelmanageapi.mapper.RoomReserveInfoMapper;
import com.llgz.hotelmanageapi.service.RoomReserveInfoServer;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客房预定信息 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@Service
public class RoomReserveInfoServiceImpl extends ServiceImpl<RoomReserveInfoMapper, RoomReserveInfo> implements RoomReserveInfoServer {

    @Autowired
    private RoomReserveInfoMapper roomReserveInfoMapper;
    @Autowired
    private OrderInfoServiceImpl orderInfoService;
    @Autowired
    RoomInfoServiceImpl roomInfoService;

    @Override
    public Boolean addRoomReserve(RoomReserveInfoDAO roomReserveInfoDAO) {
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(roomReserveInfoDAO,orderInfo);
        System.out.println(orderInfo);
        RoomReserveInfo reserveInfo = new RoomReserveInfo();
        BeanUtils.copyProperties(roomReserveInfoDAO,reserveInfo);
        orderInfoService.save(orderInfo);
        reserveInfo.setOrderId(orderInfo.getId());
        roomReserveInfoMapper.insert(reserveInfo);
        roomInfoService.updateRoomStatusByRoomId(roomReserveInfoDAO.getRoomId(),1);
        return true;
    }

    @Override
    public Integer modifyReserveStatusByOrderId(String orderId,Integer status){
        UpdateWrapper<RoomReserveInfo> reserveInfoUpdateWrapper = new UpdateWrapper<>();
        reserveInfoUpdateWrapper.eq("order_id",orderId);
        reserveInfoUpdateWrapper.set("reserve_status",1);
        return roomReserveInfoMapper.update(null,reserveInfoUpdateWrapper);

    }

    @Override
    public Boolean cancelReserve(RoomReserveInfo roomReserveInfo) {
        roomReserveInfoMapper.updateById(roomReserveInfo);
        orderInfoService.updateOrderStatusById(roomReserveInfo.getOrderId(),3);
        return true;
    }

    @Override
    public List<List<String>> getReserveExcelHead() {
        List<List<String>> headList = new LinkedList<>();
        List<String> head0 = new LinkedList<>();
        head0.add("预定单号");
        List<String> head1 = new LinkedList<>();
        head1.add("客房编号");
        List<String> head2 = new LinkedList<>();
        head2.add("客房类型");
        List<String> head3 = new LinkedList<>();
        head3.add("入住人");
        List<String> head4 = new LinkedList<>();
        head4.add("会员编号");
        List<String> head5 = new LinkedList<>();
        head5.add("抵店时间");
        List<String> head6 = new LinkedList<>();
        head6.add("离店时间");
        List<String> head7 = new LinkedList<>();
        head7.add("联系电话");

        headList.add(head0);
        headList.add(head1);
        headList.add(head2);
        headList.add(head3);
        headList.add(head4);
        headList.add(head5);
        headList.add(head6);
        headList.add(head7);
        return headList;
    }

    @Override
    public List<List<Object>> getReserveDataByDateOrRoomId(String startDayTime, String endDayTime, Integer roomId) {
        QueryWrapper<RoomReserveInfo> roomReserveInfoQueryWrapper = new QueryWrapper<>();
        roomReserveInfoQueryWrapper.apply("UNIX_TIMESTAMP(created_time) >= UNIX_TIMESTAMP('" + startDayTime + "')");
        roomReserveInfoQueryWrapper.apply("UNIX_TIMESTAMP(created_time) < UNIX_TIMESTAMP('" + endDayTime + "')");
        roomReserveInfoQueryWrapper.eq("reserve_status", 0);
        List<RoomReserveInfo> roomReserveInfoList = roomReserveInfoMapper.selectList(roomReserveInfoQueryWrapper);


        if(roomReserveInfoList.size()==0){
            return new LinkedList<>();
        }

        List<String> orderIdList = roomReserveInfoList.stream().map(RoomReserveInfo::getOrderId).collect(Collectors.toList());


        List<OrderInfo> orderInfoList = orderInfoService.listByIds(orderIdList);
        HashMap<String, OrderInfo> orderMap = new HashMap<>();
        orderInfoList.forEach(orderInfo -> orderMap.put(orderInfo.getId(), orderInfo));


        List<List<Object>> allRowDataList = new LinkedList<>();

        for (RoomReserveInfo roomReserveInfo : roomReserveInfoList) {
            List<Object> rowData = new LinkedList<>();
            OrderInfo orderInfo = orderMap.get(roomReserveInfo.getOrderId());
            // roomId筛选
            if (roomId != null && !orderInfo.getRoomId().equals(roomId)) {
                continue;
            }
            rowData.add(roomReserveInfo.getId()); // 预定单号
            rowData.add(orderInfo.getRoomId()); // 客房编号
            rowData.add(orderInfo.getRoomType()); // 客房类型
            rowData.add(roomReserveInfo.getReservePerson()); // 入住人
            rowData.add(orderInfo.getMemberId()); // 会员编号
            rowData.add(orderInfo.getArrivedTime()); // 抵店时间
            rowData.add(orderInfo.getDepartureTime()); // 离店时间
            rowData.add(orderInfo.getContactTel()); // 联系电话

            allRowDataList.add(rowData);
        }
        return allRowDataList;
    }
}
